//
//  PhotoClass.m
//  MierMilitary
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PhotoClass.h"

@implementation PhotoClass

//-(void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//
//}

//初始化
-(instancetype) initWithFrame:(CGRect)frame withArray:(NSArray *)array  withArrayTitle:(NSArray *)arrayTitle withArrayId:(NSArray *)arrayId withTopModelArray:(NSMutableArray *)topModelArray{
    self = [super initWithFrame:frame];
    if (self) {
        _array = [[NSMutableArray alloc] initWithArray:array];
        _arrayTitle = [[NSMutableArray alloc] initWithArray:arrayTitle];
        _arrayId = [[NSMutableArray alloc] initWithArray:arrayId];
        self.topModelArray = [NSMutableArray arrayWithArray:topModelArray];
        self.backgroundColor = [UIColor whiteColor];
        self.num = 0;
        [self createScollView];
      //定时器
       self.time = [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(timer) userInfo:nil repeats:YES];
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
        
    }
    return self;
}

//创建轮播图
-(void)createScollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    if (self.topModelArray.count) {
        [self.topModelArray insertObject:[self.topModelArray lastObject] atIndex:0];
        [self.topModelArray addObject:self.topModelArray[1]];
    }
    
    if (self.array.count) {
        
        [_array insertObject:[self.array lastObject] atIndex:0];
        [_arrayTitle insertObject:[self.arrayTitle lastObject] atIndex:0];
        [_arrayId insertObject:[self.arrayId lastObject] atIndex:0];

        [_array addObject:self.array[1]];
        [_arrayTitle addObject:self.arrayTitle[1]];
        [_arrayId addObject:self.arrayId[1]];
        
        for (int i = 0; i < _array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:_array[i]] placeholderImage:[UIImage imageNamed:@"ImagePlace"]];
//            [imageView nightMode];
            imageView.tag = 100 + i;
            imageView.contentMode = UIViewContentModeScaleToFill;
            imageView.clipsToBounds = YES;
            imageView.userInteractionEnabled = YES;
            
            //    1.点击（tap）
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            //    执行方法需要点击的次数
            tap.numberOfTapsRequired = 1;
            //    需要几个手指点击
            tap.numberOfTouchesRequired = 1;

            [imageView addGestureRecognizer:tap];

            [self.scrollView addSubview:imageView];
     
        }
    }
    _scrollView.contentSize = CGSizeMake(self.frame.size.width * _array.count, 0);
    _scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    _scrollView.delegate = self;

    //下面点
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, WIDTH, 20)];
    _page.numberOfPages = _array.count - 2;
    [_page addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    _page.currentPageIndicatorTintColor = kColor;
    _page.pageIndicatorTintColor = [UIColor whiteColor];
    //标题
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, self.frame.size.width * 3 / 4 , 40)];
    _label.font = [UIFont systemFontOfSize:20];
    _label.text = _arrayTitle[1];
    _label.textColor = [UIColor whiteColor];
    
    [self addSubview:self.scrollView];
    [self addSubview:self.page];
    [self addSubview:self.label];
      
}
//开始滑动 取消定时器 开始动画
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    [self.time invalidate];
    
    self.carouseType = fade;
    NSString *animationType = [self carouseAnimationType:self.carouseType];
    if (animationType != nil) {
        CATransition *animation = [CATransition animation];
        animation.duration = 1;
        animation.repeatCount = 1;
        animation.type = animationType;
        [scrollView.layer addAnimation:animation forKey:nil];
    }
    
}

//结束滑动 开启计时器
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * (self.array.count - 2), 0);
    }
    if (scrollView.contentOffset.x == self.frame.size.width *(self.array.count - 1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    

    self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    self.label.text = _arrayTitle[self.page.currentPage + 1];
    
   self.time = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timer) userInfo:nil repeats:YES];
  
}

//点对应点击方法
- (void)pageControlChange:(UIPageControl *)sender {

    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (sender.currentPage + 1), 0) animated:YES];
    
    self.label.text = _arrayTitle[sender.currentPage + 1];
}

//计时器方法及动画
- (void)timer {
    self.num++;
    
    if (self.num == _array.count - 2) {
        self.num = 0;
    }
    self.page.currentPage = self.num;
    
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.page.currentPage + 1), 0) animated:YES];
    
    self.label.text = _arrayTitle[self.page.currentPage + 1];
    //动画
    self.carouseType = rippleEffect;
    NSString *animationType = [self carouseAnimationType:self.carouseType];
    if (animationType != nil) {
        CATransition *animation = [CATransition animation];
        animation.duration = 1;
        animation.repeatCount = 1;
        animation.type = animationType;
        [self.scrollView.layer addAnimation:animation forKey:nil];
    }
}

#pragma mark -- neight

- (void)changeColor:(NSNotification *)message {
    if ([message.object
         isEqualToString:@"Night"]) {
//        self.backgroundColor = [UIColor grayColor];
//        self.label.textColor = [UIColor blackColor];
//        _page.pageIndicatorTintColor = [UIColor blackColor];
        for (int i = 0; i < self.array.count; i++) {
            UIImageView *scrollView = (UIImageView *)[self viewWithTag:100 + i];
            [scrollView nightMode];
        }
        
    } else {
//        self.backgroundColor = [UIColor whiteColor];
//        self.label.textColor = [UIColor grayColor];
//        _page.pageIndicatorTintColor = [UIColor grayColor];
        for (int i = 0; i < self.array.count; i++) {
            UIImageView *scrollView = (UIImageView *)[self viewWithTag:100 + i];
            [scrollView nightMode];
        }
    }
}

//点击手势
- (void)tapClick:(UITapGestureRecognizer *)tap {

    NSInteger i = self.scrollView.contentOffset.x / self.frame.size.width;
    if ([self.kind isEqualToString:@"食谱"]) {
        [self.delegate picPushWebView:self.arrayTitle[i] withArrayId:self.arrayId[i]];
    } else {
        [self.delegate pushNextViewController:self.arrayTitle[i] withArrayId:self.arrayId[i] withTopModel:self.topModelArray[i]];
    }
}

#pragma mark 处理枚举类型数据: 转换为字符串
- (NSString *)carouseAnimationType:(CarouselType)carouseType
{
    NSString *type = nil;
    switch (carouseType) {
        case defaults:
            type = nil;
            break;
        case cube:
            type = @"cube";
            break;
        case moveIn:
            type = @"moveIn";
            break;
        case reveal:
            type = @"reveal";
            break;
        case fade:
            type = @"fade";
            break;
        case pageCurl:
            type = @"pageCurl";
            break;
        case pageUnCurl:
            type = @"pageUnCurl";
            break;
        case suckEffect:
            type = @"suckEffect";
            break;
        case rippleEffect:
            type = @"rippleEffect";
            break;
        case oglFlip:
            type = @"oglFlip";
            break;
        case rotate:
            type = @"rotate";
            break;
        case push:
            type = @"push";
            break;
        case cameraIrisHollowOpen:
            type = @"cameraIrisHollowOpen";
            break;
        case cameraIrisHollowClose:
            type = @"cameraIrisHollowClose";
            break;
        default:
            type = nil;
            break;
    }
    return type;
}


@end
